<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

abstract class Toast extends Controller
{
	// The folder INSIDE /controllers/ where the test classes are located
	// TODO: autoset
	protected $test_dir ="/core/";

	protected $modelname;
	protected $modelname_short;
	protected $message;
        protected $user_msg;
	protected $messages;
	protected $asserts;
        protected $timings;

	public function  __construct($file_name,$module="core")
	{
            //error_reporting(0);
		parent::Controller();
                
		$this->load->library('unit_test');
		$this->modelname = $file_name;
                $this->test_dir = "/".$module."/tests/";
		$this->modelname_short = basename($file_name, '.php');
		$this->messages = array();
                $this->timings = array();
	}

	public function index()
	{
		$this->_show_all();
	}

	public function show_results()
	{
		$this->_run_all();
		$data['modelname'] = $this->modelname;
		$data['results'] = $this->unit->result();
		$data['messages'] = $this->messages;
		$this->load->view('unit_tests_views/results', $data);
	}

	public function _show_all()
	{
            
		$this->_run_all();
		$modelname = $this->modelname;
		$results = $this->unit->result();
		$messages = $this->messages;
                $timings = $this->timings;                
                $f=0;
                foreach($results as $result){
                    if($result['Result']=='Failed'){
                        $f++;
                    }
                }
                $total_failed = $f;
                $total_passed = count($results) - $f;
                require_once APPPATH.'modules/core/views/unit_tests_views/header.php';
                require_once APPPATH.'modules/core/views/unit_tests_views/results.php';
                require_once APPPATH.'modules/core/views/unit_tests_views/footer.php';

	}

	protected function _show($method)
	{
		$this->_run($method);
		$data['modelname'] = $this->modelname;
		$data['results'] = $this->unit->result();
		$data['messages'] = $this->messages;
                $data['timings'] = $this->timings;
                
		$this->load->view('unit_tests_views/header');
		$this->load->view('unit_tests_views/results', $data);
		$this->load->view('unit_tests_views/footer');
	}

	protected function _run_all()
	{
		foreach ($this->_get_test_methods() as $method)
		{
			$this->_run($method);
		}
	}

	protected function _run($method)
	{
		// Reset message from test
		$this->message = '';
                $this->user_msg = '';

		// Reset asserts
		$this->asserts = TRUE;

		$this->benchmark->mark('start');		
		$this->_pre();		
                $this->$method();
		$this->_post();
                $this->benchmark->mark('end');
                $this->timings[] = $this->benchmark->elapsed_time('start', 'end');

		$this->load->helper('url');
		$test_class_segments = $this->test_dir . strtolower($this->modelname_short);
		$test_method_segments = $test_class_segments . '/' . substr($method, 5);
		$desc = anchor($test_class_segments, $this->modelname_short) . ' -> ' . anchor($test_method_segments, substr($method, 5));
                $msg = "";
                if(strlen($this->message)){
                    $this->messages[] = $this->message. (strlen($this->user_msg) ? "<br/>".$this->user_msg:"");
                }else{
                    $this->messages[] = $this->user_msg;
                }
		

		// Pass the test case to CodeIgniter
		$this->unit->run($this->asserts, TRUE, $desc);
	}

	protected function _get_test_methods()
	{
		$methods = get_class_methods($this);
		$testMethods = array();
		foreach ($methods as $method) {
			if (substr(strtolower($method), 0, 5) == 'test_') {
				$testMethods[] = $method;
			}
		}
		return $testMethods;
	}

	/**
	 * Remap function (CI magic function)
	 * 
	 * Reroutes any request that matches a test function in the subclass
	 * to the _show() function.
	 * 
	 * This makes it possible to request /my_test_class/my_test_function
	 * to test just that single function, and /my_test_class to test all the
	 * functions in the class.
	 * 
	 */
	public function _remap($method)
	{
		$test_name = 'test_' . $method;
		if (method_exists($this, $test_name))
		{
			$this->_show($test_name);
		}
		else
		{
			$this->$method();
		}
	}


	/**
	 * Cleanup function that is run before each test case
	 * Override this method in test classes!
	 */
	public function _pre() { }

	/**
	 * Cleanup function that is run after each test case
	 * Override this method in test classes!
	 */
	public function _post() { }


	protected function _assert_true($assertion) {
		if($assertion) {
			return TRUE;
		} else {
			$this->asserts = FALSE;
                        $this->message = "Expected TRUE  ";
			return FALSE;
		}
	}
	
	protected  function _assert_false($assertion) {
		if($assertion) {
			$this->asserts = FALSE;
                        $this->message = "Expected FALSE  recieved ".$assertion;
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
	protected  function _assert_equals($base, $check) {
		if($base == $check) {
			return TRUE;
		} else {
			$this->asserts = FALSE;
                        $this->message = "Expected ".  $base. " recieved ".$check;
			return FALSE;
		}
	}
	
	protected  function _assert_not_equals($base, $check) {
		if($base != $check) {
			return TRUE;
		} else {
			$this->asserts = FALSE;
                        $this->message = "DID NOT Expect $base  recieved ".$check;
			return FALSE;
		}
	}

	protected  function _assert_equals_strict($base, $check) {
		if($base === $check) {
			return TRUE;
		} else {
			$this->asserts = FALSE;
                        $this->message = "Strictly Expected $base  recieved ".$check;
			return FALSE;
		}
	}

	protected  function _assert_not_equals_strict($base, $check) {
		if($base !== $check) {
			return TRUE;
		} else {
			$this->asserts = FALSE;
                        $this->message = "Strictly NOT Expected $base  recieved ".$check;
			return FALSE;
		}
	}

	protected  function _assert_empty($assertion) {
		if(empty($assertion)) {
			return TRUE;
		} else {
			$this->asserts = FALSE;
                         $this->message = "Expected empty  recieved ".$assertion;
			return FALSE;
		}
	}
	
	protected  function _assert_not_empty($assertion) {
		if(!empty($assertion)) {
			return TRUE;
		} else {
			$this->asserts = FALSE;
                        $this->message = "DID NOT Expect empty  recieved empty";
			return FALSE;
		}
	}
	

}

// End of file Toast.php */
// Location: ./system/application/controllers/test/Toast.php */ 
